
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
react-intersection-observer-hook
Advanced tools
This is a simple to use React hook package for using Insersection Observer declaratively. By using this hook, you can easily track if a component is visible or not, create lazy loading images, trigger animations on entering or leaving the viewport, implement infinite loading etc.
Live demo is here.
This package relies on Intersection Observer API. Browser compatibility can be seen in here.
If you want to support the browsers those are not supporting it natively, you can use a polyfill.
npm install react-intersection-observer-hook
import React, { useEffect } from 'react';
import { useIntersectionObserver } from 'react-intersection-observer-hook';
// ...
function Example() {
// `useIntersectionObserver` returns a tuple.
// We need to give this `ref` callback to the node we want to observe.
// The second item, `entry` is the response of the `IntersectionObserver` instance.
const [ref, { entry }] = useIntersectionObserver();
const isVisible = entry && entry.isIntersecting;
useEffect(() => {
console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`);
}, [isVisible]);
return <SomeComponentToTrack ref={ref} />;
}
If you have a scrollable container, you can set a root
like this:
import React, { useEffect } from 'react';
import { useIntersectionObserver } from 'react-intersection-observer-hook';
// ...
function Example() {
const [ref, { entry, rootRef }] = useIntersectionObserver();
const isVisible = entry && entry.isIntersecting;
useEffect(() => {
console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`);
}, [isVisible]);
return (
<ScrollableContainer
// We use `rootRef` callback to set the root node.
ref={rootRef}
>
<SomeComponentToTrack ref={ref} />
</ScrollableContainer>
);
}
If you just want to track visibility, you can also use useTrackVisibility
hook.
It has the same API as useIntersectionObserver
hook. It just returns additional fields as its second tuple item.
import React, { useEffect } from 'react';
import { useTrackVisibility } from 'react-intersection-observer-hook';
// ...
function Example() {
// `useTrackVisibility` also returns a tuple like `useIntersectionObserver`.
// First item is the same `ref` callback to set the node to observe.
// Second item is an object that we can use to decide if a node is visible.
// `entry`: Same object which is returned by `useIntersectionObserver`.
// `rootRef`: Same ref callback which is returned by `useIntersectionObserver`.
// `isVisible`: Becomes `true`/`false` based on the response of `IntersectionObserver`.
// `wasEverVisible`: When the observed node becomes visible once, this flag becomes `true` and stays like that.
const [ref, { entry, rootRef, isVisible, wasEverVisible }] =
useTrackVisibility();
useEffect(() => {
console.log(`The component is ${isVisible ? 'visible' : 'not visible'}.`);
}, [isVisible]);
return <SomeComponentToTrack ref={ref} />;
}
You can find more usage examples in the demo
app in this repository.
Both useIntersectionObserver
and useTrackVisibility
gets the same arguments. And those are;
For more info, you can check here and here.
Thanks goes to these wonderful people (emoji key):
KimSeonghyeon 💻 |
|
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
React hook to use IntersectionObserver declaratively.
The npm package react-intersection-observer-hook receives a total of 94,320 weekly downloads. As such, react-intersection-observer-hook popularity was classified as popular.
We found that react-intersection-observer-hook demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.